package com.judysen.jsqlweb.aop;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggerAop {
    private Logger logger = LoggerFactory.getLogger(LoggerAop.class);

    @Pointcut("@annotation(com.judysen.jsqlweb.aop.JsqlLoggerAnnotation)")           //包下所有的方法
    private void loggerPointcut() {
    }

    @Pointcut("execution(public * com.judysen.jsqlweb.services..*.*(..))")
    private void servicesPointcut() {
    }

    @Around("loggerPointcut()")
    private Object loggerAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        try {
            var params = proceedingJoinPoint.getArgs();
            MethodSignature ms = (MethodSignature) proceedingJoinPoint.getSignature();
//            logger.info(String.format("%s start [%s]", ms.getMethod().getName(), JSON.toJSONString(params)));
            var rnt = proceedingJoinPoint.proceed();
//            logger.info(String.format("%s end,[%s]", ms.getMethod().getName(), JSON.toJSONString(params)));
            return rnt;
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            throw ex;
        }
    }
}
